import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import {useStore} from 'vuex'

import Layout from '../views/Main.vue'
import Login from '../views/login/Login.vue'
import Admin from '../views/auth/Admin.vue'
import Group from '../views/auth/Group.vue'
import Order from '../views/vppz/Order.vue'
import Staff from '../views/vppz/Staff.vue'
import Dashboard from '../views/dashboard/Dashboard.vue'
import DailyCapacity from '../views/statistics/DailyCapacity.vue'
import LgMaterialBid from '../views/statistics/lg_materialBid.vue'
import BaoGong from '../views/statistics/BaoGong.vue'
import SalesRebate from '../views/sales/SalesRebate.vue'
import PanDianDetail from '../views/report/PanDianDetail.vue'
import ReportList from '../views/report/ReportList.vue'
import InvoiceNotice from "../views/purchase/InvoiceNotice.vue";

const routes = [
    {
        path: '/',
        component: Layout,
        name: 'main',
        children: [
            {
                path: 'dashboard',
                meta: {
                    id: '1',
                    name: '控制台',
                    icon: 'Platform',
                    path: '/dashboard',
                    describe: '用于展示当前系统中的统计数据、统计报表及重要实时数据',
                    requiresAuth: true
                },
                component: Dashboard
            },
            {
                path: 'statistics',
                meta: {id: '4', name: '统计', icon: 'BellFilled'},
                children: [
                    {
                        path: '',
                        alias: ['dailyCapacity'],
                        meta: {
                            id: '1',
                            name: '每日产量',
                            icon: 'Checked',
                            path: '/statistics/dailyCapacity',
                            describe: '统计每日产量'
                        },
                        component: DailyCapacity
                    },
                    {
                        path: '',
                        alias: ['lgMaterialBid'],
                        meta: {
                            id: '2',
                            name: '材料报价',
                            icon: 'Checked',
                            path: '/statistics/lgMaterialBid',
                            describe: '材料报价'
                        },
                        component: LgMaterialBid
                    },
                    {
                        path: '',
                        alias: ['baoGong'],
                        meta: {id: '32', name: '报工', icon: 'Checked', path: '/statistics/baoGong', describe: '报工'},
                        component: BaoGong
                    }
                ]
            },
            {
                path: 'sales',
                meta: {id: '3', name: '销售', icon: 'Grid'},
                children: [
                    {
                        path: 'salesRebate',
                        meta: {
                            id: '1',
                            name: '销售返点',
                            icon: 'Menu',
                            path: '/sales/salesRebate',
                            describe: '销售返点,同时菜单栏数据也从规则中获取'
                        },
                        component: SalesRebate
                    }
                ]
            },
            {
                path: 'report',
                meta: {id: '5', name: '报表', icon: 'Grid'},
                children: [
                    {
                        path: 'reportList',
                        meta: {
                            id: '2',
                            name: '报表列表',
                            icon: 'Menu',
                            path: '/report/reportList',
                            describe: '报表列表'
                        },
                        component: ReportList
                    }
                ]
            },
            {
                path: 'purchase',
                meta: {id: '6', name: '采购', icon: 'Grid'},
                children: [
                    {
                        path: 'invoiceNotice',
                        meta: {
                            id: '1',
                            name: '已开采购查询',
                            icon: 'Menu',
                            path: '/purchase/invoiceNotice',
                            describe: '采购开票通知单'
                        },
                        component: InvoiceNotice
                    }
                ]
            },
            {
                path: 'auth',
                meta: {id: '2', name: '权限管理', icon: 'Grid'},
                children: [
                    {
                        path: '',
                        alias: ['admin'],
                        meta: {
                            id: '1',
                            name: '账号管理',
                            icon: 'Avatar',
                            path: '/auth/admin',
                            describe: '管理员可以进行编辑，权限修改后需要登出才会生效'
                        },
                        component: Admin
                    },
                    {
                        path: 'group',
                        meta: {
                            id: '2',
                            name: '菜单管理',
                            icon: 'Menu',
                            path: '/auth/group',
                            describe: '菜单规则通常对应一个控制器的方法,同时菜单栏数据也从规则中获取'
                        },
                        component: Group
                    }
                ]
            },
            // {
            //   path: 'vppz',
            //   meta: { id: '3', name: 'DIDI陪诊', icon: 'BellFilled' },
            //   children: [
            //     {
            //       path: '',
            //       alias: ['staff'],
            //       meta: { id: '1', name: '陪护管理', icon: 'Checked', path: '/vppz/staff', describe: '陪护师可以进行创建和修改，设置对应生效状态控制C端选择' },
            //       component: Staff
            //     },
            //     {
            //       path: 'order',
            //       meta: { id: '2', name: '订单管理', icon: 'List', path: '/vppz/order', describe: 'C端下单后可以查看所有订单状态，已支付的订单可以完成陪护状态修改' },
            //       component: Order
            //     }
            //   ]
            // },

        ]
    },
    {
        path: '/login',
        meta: {requiresUnauth: true},
        component: Login
    },
]

const router = createRouter({
    routes,
    history: createWebHistory('/lianguivue/')
})

router.beforeEach((to, from, next) => {
    // 检查用户是否已认证
    const isAuthenticated = localStorage.getItem("token") != null
    // console.log('to', to)
    // console.log('from', next)
    // console.log('next', next)

    // 检查路由路由需要认证
    // debugger
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
    const requiresUnauth = to.matched.some(record => record.meta.requiresUnauth)
    const store = useStore()
    if (!requiresUnauth && !isAuthenticated) {
        // 携带重定向路径

        // next({path: '/login', query: {redirect: to.fullPath}})
    } else if (requiresUnauth && isAuthenticated) {
        const redirect = to.query.redirect
        if (redirect) {
            next(redirect) // 如果是登陆界面，但是有重定向，则进入重定向界面
        } else {
            next('/dashboard') // 已登陆用户禁止访问登陆页，直接进入到主页
        }
        store.dispatch('auth/fetchUser')
    } else {
        store.dispatch('auth/fetchUser')
        next() // 正常放行
    }
})

export default router
